package com.photoeditor.demo.model.bean;

import java.util.Date;

public class DateBean {
	
	private int mYear;
	private int mMonth;
	private int mDay;
	private long mDateTime;
	
	public DateBean(long dateTime){
		Date d = new Date(dateTime);
		mDateTime = dateTime;
		mYear = 1900 + d.getYear();
		mMonth = 1 + d.getMonth();
		mDay = d.getDate();
	}

	public DateBean(){
	}

	public void setDate(long dateTime){
		Date d = new Date(dateTime);
		mDateTime = dateTime;
		mYear = 1900 + d.getYear();
		mMonth = 1 + d.getMonth();
		mDay = d.getDate();
	}

	public int getYear() {
		return mYear;
	}

	public int getMonth() {
		return mMonth;
	}

	public int getDay() {
		return mDay;
	}

	public boolean isSameDay(DateBean bean){
		if((bean.getDay() == mDay && bean.getYear() == mYear && bean.getMonth() == mMonth) || mDateTime >= bean.mDateTime){
			return true;
		}
		return false;
	}
	
	public boolean isSameMonth(DateBean bean){
		if(bean.getMonth() == mMonth && bean.getYear() == mYear){
			return true;
		}
		return false;
	}
	
	public boolean isSameYear(DateBean bean){
		if(bean.getYear() == mYear){
			return true;
		}
		return false;
	}
	
	public boolean isSameDay(long time){
		DateBean bean = new DateBean(time);
		if((bean.getDay() == mDay && bean.getYear() == mYear && bean.getMonth() == mMonth) || mDateTime >= bean.mDateTime){
			return true;
		}
		return false;
	}
	
	public boolean isSameMonth(long time){
		DateBean bean = new DateBean(time);
		if(bean.getMonth() == mMonth && bean.getYear() == mYear){
			return true;
		}
		return false;
	}
	
	public boolean isSameYear(long time){
		DateBean bean = new DateBean(time);
		if(bean.getYear() == mYear){
			return true;
		}
		return false;
	}
	
	public String getYearMonthString(){
		StringBuilder sb = new StringBuilder();
		sb.append(mYear);
		sb.append('-');
		if(mMonth < 10){
			sb.append(0).append(mMonth);
		} else{
			sb.append(mMonth);
		}
		return sb.toString();
	}
	
	public long getDateTime(){
		return mDateTime;
	}
	
	/**
	 * 这个方法用于广告判断是不是当天
	 * @return
	 */
	public boolean isToday(){
		DateBean bean = new DateBean(System.currentTimeMillis());
		if((bean.getDay() == mDay && bean.getYear() == mYear && bean.getMonth() == mMonth)){
			return true;
		}
		return false;
	}

	/**
	 * @param nowTime
	 * @return
	 */
	public boolean isYesterday(DateBean nowTime){
		DateBean yesterday = new DateBean(nowTime.getDateTime() - (24 * 60 * 60 * 1000));
		return this.isSameDay(yesterday);
	}

	/**
	 * 是否在days天之内  判断当前的日子是不是在Bean的前Days天之内
	 * @param bean 传入今天的Bean
	 * @param days
	 * @return
	 */
	public boolean isDurationDays(DateBean bean, int days){
		Date date = new java.sql.Date(mYear - 1900, mMonth - 1, mDay);
		DateBean cache = new DateBean(date.getTime());
		float d = (bean.getDateTime() - cache.getDateTime()) * 1.0f / 1000 / 60 / 60 / 24;
		int nowDays = (int)(Math.ceil(d));
		if(nowDays > 0 && nowDays <= 7){
			return true;
		}
		return false;
	}

	public String getMonthDayString(){
		StringBuilder sb = new StringBuilder();
		if(mMonth < 10){
			sb.append(0).append(mMonth);
		} else{
			sb.append(mMonth);
		}
		sb.append('-');
		if(mDay < 10){
			sb.append(0).append(mDay);
		} else{
			sb.append(mDay);
		}
		return sb.toString();
	}
}
